/**
 * Created by kevin on 2017/6/7.
 */
var model= require('./model');

var Delegaters = model.createModel({
    name : String,
    type : Number, // 1 ios 2 android
    ip : String,
    user : String,
    port : {type : Number,default : 22},
    platform : Number, // 1 mac 2 linux
    packageCount : {type : Number,default : 0},
    packageSpend : {type : Number,default : 0},
    prefixScript : {type : String,default : ''},
    suffixScript : {type : String,default : ''},
    ctime : {type : Date,default:Date.now}
},'Delegaters');

Delegaters.getAndroidList = function(callback) {
    Delegaters.find({type:2},null,{ lean: true ,sort:{ctime:-1}},callback);
};

Delegaters.getIOSList = function(callback) {
    Delegaters.find({type:1},null,{ lean: true ,sort:{ctime:-1}},callback);
};

Delegaters.getListByType = function (type,callback) {
    Delegaters.find({type:type},null,{ lean: true ,sort:{ctime:-1}},callback);
};

Delegaters.getAll = function(callback) {
    Delegaters.find({},null,{ lean: true ,sort:{ctime:-1}},callback);
};

Delegaters.getById = function (id,callback) {
    Delegaters.findById(id,callback);
};

Delegaters.createUniqueName = function (doc,callback) {
    Delegaters.findOne({name : doc.name},function (err,delegater) {
        if (delegater) {
            if (callback) {
                callback(new Error('代理名称已被占用',null));
            }
        } else  {
            Delegaters.create(doc,callback);
        }
    });
};

Delegaters.edit = function (id,doc,callback) {
    Delegaters.findByIdAndUpdate(id, doc, callback);
};
module.exports= Delegaters;
